/**
 * 
 */
package com.shunwang.io.stream;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import junit.framework.TestCase;

/**
 * @author ljw
 *
 */
public class FileInputStreamTest extends TestCase {

	public void testStreamReader() throws IOException{
		FileInputStream fis = new FileInputStream("d:/aaaa/test/1.txt");
		byte[] bytes = new byte[5];
		long begin = System.currentTimeMillis();
		for(int i=0;i<1000000;i++){
			readInputStream(fis,bytes);
		}
		System.out.println("readInputStream cost time:"+(System.currentTimeMillis() - begin));
	}
	
	public void testChannelReader() throws IOException{
		FileInputStream fis = new FileInputStream("d:/aaaa/test/1.txt");
		ByteBuffer buffered = ByteBuffer.allocate(5);
		long begin = System.currentTimeMillis();
		for(int i=0;i<1000000;i++){
			readChannel(fis,buffered);
		}
		System.out.println("readChannel cost time:"+(System.currentTimeMillis() - begin));
	}
	
	public void testStreamReaderBuffered() throws IOException{
		InputStream fis = new BufferedInputStream(new FileInputStream("d:/aaaa/test/1.txt"),5);
		byte[] bytes = new byte[10000];
		long begin = System.currentTimeMillis();
		for(int i=0;i<1000000;i++){
			readInputStream(fis,bytes);
		}
		System.out.println("readInputStreamBuffered cost time:"+(System.currentTimeMillis() - begin));
	}
	
	private void readInputStream(InputStream fis,byte[] content) throws IOException{
		fis.read(content);//0,(content.length)
	}
	
	private void readChannel(FileInputStream fis,ByteBuffer bb) throws IOException{
		FileChannel channel = fis.getChannel();
		channel.read(bb);
	}
}
